package com.wdh.kjs.util;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.MobileDeleteItemFileModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyh on 2016/8/26 0026.
 * 用于删除已经上传过的文件
 */
public class DeleteFileUtil {
    /**
     * 删除已经上传的融资文件
     *
     * @return
     */
    public static void DeletRongziFile(ArrayList<String> deleteIdList) {
        for (String str : deleteIdList) {
            RequestParams params = new RequestParams();
            params.addBodyParameter("KjsItemInfoDocumentId", str);
            HttpUtils http = new HttpUtils();
            http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_PICTURE_DELETE, params, new RequestCallBack<String>() {

                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Gson gson = new Gson();
                    MobileDeleteItemFileModel mdfm = gson.fromJson(responseInfo.result, MobileDeleteItemFileModel.class);
                   /* if(mdfm.isSuccess()==true){
                        System.out.println("已经删除成功");
                    }else {
                        System.out.println("删除失败"+mdfm.getMsg());
                    }*/
                }

                @Override
                public void onFailure(HttpException error, String msg) {
                    System.out.println("网络异常!");
                }
            });
        }
    }

    /**
     * @param allFilePathList:所有文件路径
     * @param deleteFilePathList:删除文件的路径
     * @return 剩余的文件的路径
     */
    public static ArrayList<String> getRemainFile(List<String> allFilePathList, List<String> deleteFilePathList) {
        ArrayList<String> remainFileList = new ArrayList<>();
        for (String str : allFilePathList) {
            remainFileList.add(str);
        }
       /* System.out.println("删除的图片名称:");*/
        for (String str : deleteFilePathList) {
            remainFileList.remove(str);
            /*System.out.println(str+"   ");*/
        }
        return remainFileList;
    }

    /**
     * @param allFileId:所有已经上传的文件的Id
     * @param delleteId:需要去掉的文件的Id
     * @return 剩下的文件的Id
     */
    public static ArrayList<String> getRemainId(String allFileId, ArrayList<String> delleteId) {
        ArrayList<String> remainId = (ArrayList<String>) JSON.parseArray(allFileId, String.class);
       // System.out.println("allFileId:"+allFileId);
       // System.out.print("删除的Id:");
        for (String id : delleteId) {
            remainId.remove(id);
       //     System.out.print(id+" : ");
        }
      //  System.out.println("");
        return remainId;
    }

    /**
     * @param allfilePath：所有已经上传文件的路径
     * @param deletefilePath：本次要删除的文件路径
     * @param allId：所有已经上传的文件对应的Id
     * @return 要删除的Id
     */
    public static ArrayList<String> getDeletId(List<String> allfilePath, ArrayList<String> deletefilePath, String allId) {
        List<String> allIdList = JSON.parseArray(allId, String.class);
        //System.out.println("allIdList:"+allId);
        ArrayList<String> deleteId = new ArrayList<>();
        List<String> arrayList=new ArrayList<>();
        for(String str:allfilePath) {
            arrayList.add(str);
        }
        int location;
        //System.out.println("所有已经上传的文件ID:"+allId);
        //System.out.print("增加删除的id:");
       // System.out.println("大小:"+allIdList.size());
        for (String path : deletefilePath) {
            location = arrayList.indexOf(path);
            String id = allIdList.get(location);
            allIdList.remove(location);
            arrayList.remove(location);
            deleteId.add(id);
        //    System.out.println("id:"+id+"\n path"+path+"\n");
        }
        //System.out.println("");
        return deleteId;
    }
}
